package cn.ctyun.videoplayer.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import cn.ctyun.videoplayer.R;
import cn.ctyun.videoplayer.dlnaupnp.entity.ClingDevice;
import cn.ctyun.videoplayer.dlnaupnp.service.manager.ClingManager;

import org.fourthline.cling.model.meta.Device;


public class DLNADevicesAdapter extends ArrayAdapter<ClingDevice> {
    private LayoutInflater mInflater;

    /*public void setConnectedDevice(Device mConnectedDevice) {
        this.mConnectedDevice = mConnectedDevice;
    }

    private Device mConnectedDevice;*/

    public DLNADevicesAdapter(Context context) {
        super(context, 0);
        mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null)
            convertView = mInflater.inflate(R.layout.dlna_devices_item, null);

        ClingDevice item = getItem(position);
        if (item == null || item.getDevice() == null) {
            return convertView;
        }

        Device device = item.getDevice();

        TextView textView = (TextView) convertView.findViewById(R.id.dlna_tv_device_name);
        textView.setText(device.getDetails().getFriendlyName());

        ProgressBar connecting = (ProgressBar) convertView.findViewById(R.id.dlna_progress_device_connecting);

        ImageView imageView = (ImageView)convertView.findViewById(R.id.dlna_tv_device_connected);

        if (ClingManager.getInstance().getSelectedDevice() != null && ((ClingDevice)ClingManager.getInstance().getSelectedDevice()).isSelected()) {
            if (device.getIdentity().equals(((Device) ClingManager.getInstance().getSelectedDevice().getDevice()).getIdentity())) {
                imageView.setVisibility(View.VISIBLE);
            } else {
                imageView.setVisibility(View.GONE);
            }
        } else {
            imageView.setVisibility(View.GONE);
        }
        return convertView;
    }
}